package rocketmq.demo.sync.trans;

import rocketmq.demo.utils.MqConfig;

import com.alibaba.rocketmq.client.producer.DefaultMQProducer;
import com.alibaba.rocketmq.client.producer.SendResult;
import com.alibaba.rocketmq.common.message.Message;
import com.alibaba.rocketmq.remoting.common.RemotingHelper;

/**
 * <pre>
 * Reliable synchronous transmission 同步传输
 * 
 * Application: Reliable synchronous
 * transmission is used in extensive scenes, such as important notification
 * messages, SMS notification, SMS marketing system, etc..
 * </pre>
 */
public class SyncProducer {
	public static void main(String[] args) throws Exception {
		// Instantiate with a producer group name.
		DefaultMQProducer producer = new DefaultMQProducer(MqConfig.GROUP_TEST2);

		// 指定nameServer的地址
		producer.setNamesrvAddr(MqConfig.DEFAULT_NAME_SRV_ADDR);

		// Launch the instance.
		producer.start();
		for (int i = 0; i < 3; i++) {
			// Create a message instance, specifying topic, tag and message
			// body.
			Message msg = new Message(MqConfig.TOPIC_SYNC /* Topic */,
					MqConfig.TAG_TEST /* Tag */,
					("Hello Sync Message RocketMQ " + i)
							.getBytes(RemotingHelper.DEFAULT_CHARSET) /*
																	 * Message
																	 * body
																	 */
			);
			// Call send message to deliver message to one of brokers.
			SendResult sendResult = producer.send(msg);
			System.out.printf("%s%n", sendResult);
		}
		// Shut down once the producer instance is not longer in use.
		producer.shutdown();
	}
}
